// 当前等待的渲染列表
import 'dart:async';

import '../compute.dart';
import 'tools.dart';
import '../watch.dart';
import 'update.dart';

List<dynamic> _effectWidgetList = [];
dynamic _effectTimer;

_excute(List<dynamic> tempList) {
  tempList = listUnRepeat(tempList);
  tempList.forEach((ele) {
    try {
      if (ele is WatchCollectFactory) {
        ele.instance.excute();
      } else if (ele is ComputeComputeFactory) {
        ele.instance.excute();
      }
    } catch (e) {
      print('app wrong: Ref effect 调用instance.sink.add(refInstance)出错了！');
    }
  });
}

// 执行副作用
excuteEffect<T>(List<T> effectList, [UpdateOptions? options]) {
  if (options != null && options.delay != null && options.delay is Duration) {
    if (_effectTimer != null && _effectTimer.isActive) _effectTimer.cancel();
    _effectWidgetList.addAll(effectList);
    _effectTimer = Timer(options.delay as Duration, () {
      _excute(_effectWidgetList);
      _effectWidgetList.clear();
    });
  } else {
    _excute(listUnRepeat(effectList));
  }
}
